package com.blb.zy_community.controller;

import com.alibaba.fastjson.JSON;
import com.blb.zy_community.common.enmus.BussinessType;
import com.blb.zy_community.entity.ResponseResult;
import com.blb.zy_community.entity.SysMenu;
import com.blb.zy_community.service.ISysMenuService;
import com.blb.zy_community.utils.MyLog;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 菜单权限表 前端控制器
 * </p>
 *
 * @author 二组
 * @since 2023-06-18
 */
@CrossOrigin
@RestController
public class SysMenuController {
    @Autowired
    private ISysMenuService sysMenuService;

    /**
     * 根据登录用户不同返回不同的菜单树
     * @return
     */
//    @MyLog("get")
    @GetMapping("/sysMenu/treeByusername")
    public ResponseResult<SysMenu> getSysMenuTreeByUsername(@RequestParam String username) {
        List<SysMenu> sysMenuTree= sysMenuService.getSysMenuTreeByUsername(0L,username);
        String jsonString = JSON.toJSONString(sysMenuTree);
        System.out.println(jsonString);
        return ResponseResult.ok(sysMenuTree);
    }


    /**
     * 返回系统权限树（多一个根目录）
     *
     * @return
     */
    @GetMapping("/sysMenu/treeWithRoot")
    public ResponseResult<SysMenu> getSysMenuTreeWithRoot() {
        List<SysMenu> sysMenuTree = sysMenuService.getSysMenuTree(0L);
        SysMenu sysMenu = new SysMenu();
        sysMenu.setMenuId(0L);
        sysMenu.setMenuName("主类目");
        sysMenu.setChildren(sysMenuTree);
        ArrayList<SysMenu> sysMenuTree2 = new ArrayList<>();
        sysMenuTree2.add(sysMenu);
        String jsonString = JSON.toJSONString(sysMenuTree2);
        System.out.println(jsonString);
        return ResponseResult.ok(sysMenuTree2);
    }


    /**
     * 返回系统权限树
     *
     * @return
     */
    @GetMapping("/sysMenu/tree")
    public ResponseResult<SysMenu> getSysMenuTree() {
        List<SysMenu> sysMenuTree = sysMenuService.getSysMenuTree(0L);
        String jsonString = JSON.toJSONString(sysMenuTree);
        System.out.println(jsonString);
        return ResponseResult.ok(sysMenuTree);
    }

    /**
     * 返回所有权限-列表
     *
     * @return
     */
    @GetMapping("/sysMenu/list")
    public ResponseResult<List<SysMenu>> getSysMenuList() {
        List<SysMenu> sysMenuList = sysMenuService.list();
        return ResponseResult.ok(sysMenuList);
    }

    /**
     * 添加系统菜单
     *
     * @param sysMenu
     * @return
     */
    @MyLog(title = "菜单管理", bussinessType = BussinessType.INSERT)
    @PostMapping("/sysMenu")
    public ResponseResult<SysMenu> addSysMenu(@RequestBody SysMenu sysMenu) {
        sysMenu.setCreateTime(LocalDateTime.now());
        sysMenuService.save(sysMenu);
        return ResponseResult.ok(sysMenu);
    }

    /**
     * 修改系统菜单
     *
     * @param sysMenu
     * @return
     */
    @MyLog(title = "菜单管理", bussinessType = BussinessType.UPDATE)
    @PutMapping("/sysMenu")
    public ResponseResult<SysMenu> updateSysMenu(@RequestBody SysMenu sysMenu) {
        sysMenuService.updateById(sysMenu);
        return ResponseResult.ok(sysMenu);
    }

    /**
     * 删除系统菜单
     *
     * @param menuId
     * @return
     */
    @MyLog(title = "菜单管理", bussinessType = BussinessType.DELETE)
    @DeleteMapping("/sysMenu/{menuId}")
    public ResponseResult<SysMenu> deleteSysMenu(@PathVariable("menuId") Long menuId) {
        sysMenuService.removeById(menuId);
        return ResponseResult.ok(menuId);
    }

    /**
     * 模糊查询系统菜单
     * 有问题，如果父标签不包含某些字段，就不会查子标签
     */
    @GetMapping("/sysMenu/tree/condition")
    public ResponseResult<List<SysMenu>> getSystemMenuWithCondition(@RequestParam(required = false, value = "menuName") String menuName,
                                                                    @RequestParam(required = false, value = "status") String status

    ) {
        List<SysMenu> sysMenuTree = sysMenuService.getSystemMenuTreeWithCondition(0L, menuName, status);
        return ResponseResult.ok(sysMenuTree);
    }
    /**
     * yty新增
     * 模糊查询系统菜单
     * 有问题，如果父标签不包含某些字段，就不会查子标签
     */
    @GetMapping("/sysMenu/tree/condition2")
    public ResponseResult<List<SysMenu>> getSystemMenuWithCondition2(
            @RequestParam(required = false) String menuName,
            @RequestParam(required = false) String status
    ){
        if (menuName==""||menuName==null){
            List<SysMenu> sysMenuTree = sysMenuService.getSystemMenuTreeWithCondition(0L, menuName, status);
            return ResponseResult.ok(sysMenuTree);
        }else {
            //先拿到全部数据
            List<SysMenu> sysMenuTree = sysMenuService.getSysMenuTree(0L);
            List<SysMenu> sysMenuTree2 =sysMenuService.filterByMenuNameAndStatus(sysMenuTree,menuName,status);
            return ResponseResult.ok(sysMenuTree2);
        }
    }


}
